import router from '@system.router'

var picker1value = null;
var picker2value = null;
var picker1second = null;
var picker2second = null;
var timer1 = null;
var timer2 = null;
var timer3 = null
var counter = 0;

export default {
    data: {
        second: 0,
        isShow: true,
        huxi: "吸气",
        percent: "0",
        duration: "",
        count: ""
    },
    clickAction() {
        clearInterval(timer1);
        timer1 = null;
        clearInterval(timer2);
        timer2 = null;
        clearInterval(timer3);
        timer3 = null;
        router.replace({
            uri: 'pages/index/index'
        })
    },
    onInit() {
        console.log("xunlian init");
        console.log("左边：" + this.picker1value);
        console.log("右边：" + this.picker2value);
        picker1value = this.picker1value;
        picker2value = this.picker2value;
        if (picker1value == "1") {
            picker1second = 60;
        } else if (picker1value == "2") {
            picker1second = 120;
        } else if (picker1value = "3") {
            picker1second = 180;
        }
        if (picker2value == "较慢") {
            picker2second = 6;
        } else if (picker2value == "舒缓") {
            picker2second = 4;
        } else if (picker2value == "较快") {
            picker2second = 2;
        }
        this.second = picker1second;
        this.duration = picker2second + "s";
        this.count = (picker1second / picker2second).toString();
    },
    run1() {
        this.second --;
        if (this.second == 0) {
            clearInterval(timer1);
            timer1 = null;
            this.isShow = false;
        }
    },
    run2() {
        counter ++;
        if (picker1second / picker2second == counter) {
            clearInterval(timer2);
            timer2 = null;
            this.huxi = "已完成"
        } else {
            if (this.huxi == "吸气") {
                this.huxi = "呼气";
            } else if (this.huxi = "呼气") {
                this.huxi = "吸气";
            }
        }
    },
    run3() {
        this.percent = (parseInt(this.percent) + 1).toString();
        if (parseInt(this.percent) < 10) {
            this.percent = "0" + this.percent;
        }
        if (parseInt(this.percent) == 100) {
            this.percent = "0";
        }
        if (timer2 == null) {
            clearInterval(timer3);
            timer3 = null;
            this.percent = "100";
        }
    },
    onReady() {
        console.log("xunlian ready");
    },
    onShow() {
        console.log("xunlian show");
        timer1 = setInterval(this.run1,1000);
        timer2 = setInterval(this.run2,picker2second * 1000)
        timer3 = setInterval(this.run3,picker2second / 100 * 1000)
    },
    onDestroy() {
        console.log("xunlian destroy");
    }
}
